﻿using System.Threading.Tasks;
using EduFormManager.Models;
using EduFormManager.Models.Log;
using EduFormManager.Models.Repo;

namespace EduFormManager.Utilities
{
    static class DBLogHelper
    {
        public static void Log(Credentials user, LogAction.ActionType action)
        {
            if (user.IsEdu)
            {
                Log log = new Log()
                {
                    EduOrg = user.EduOrg,
                    Action = RepoHelper.Repo.GetById<LogAction>((int)action)
                };
                RepoHelper.Repo.Save(log);
            }
        }

        public static Task LogAsync(Credentials user, LogAction.ActionType action)
        {
            return Task.Run(() => Log(user, action));
        }

        public static void LogLogin(Credentials user)
        {
            Log(user, LogAction.ActionType.Login);
        }

        public static Task LogLoginAsync(Credentials user)
        {
            return Task.Run(() => LogLogin(user));
        }

        public static void LogLogout(Credentials user)
        {
            Log(user, LogAction.ActionType.Logout);
        }

        public static Task LogLogoutAsync(Credentials user)
        {
            return Task.Run(() => LogLogout(user));
        }
    }
}
